# Build recipe for tzdb.
#
# Copyright (c) 2017-2021 Matias Fonzo, <selk@dragora.org>.
# Copyright (c) 2018 Mateus P. Rodrigues, <mprodrigues@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=tzdb
version=2021e
release=1

# Define a category for the output of the package name
pkgcategory=db

tarname=${program}-${version}.tar.lz

# Remote source(s)
fetch=https://www.iana.org/time-zones/repository/releases/$tarname

description="
Time zone database.

The Time Zone Database (often called tz or zoneinfo) contains code and
data that represent the history of local time for many representative
locations around the globe.  It is updated periodically to reflect
changes made by political bodies to time zone boundaries, UTC offsets,
and daylight-saving rules.  Its management procedure is documented in
BCP 175: Procedures for Maintaining the Time Zone Database.
"

homepage=https://www.iana.org/time-zones
license="Public domain | BSD"

# Source documentation
docs="CONTRIBUTING LICENSE NEWS README"
docsdir="${docdir}/${program}-${version}"

# Force parallel jobs to '1'
jobs=1

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    make -j${jobs} CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS -static" \
     TOPDIR=/ \
     TZDIR=/etc/zoneinfo \
     ETCDIR=/usr/sbin \
     LIBDIR=/usr/lib${libSuffix} \
     MANDIR=$mandir \
     KSHELL="/bin/sh -" \
     DESTDIR="$destdir" install

    # 1. We do not offer 'tzselect'.
    # 2. To set default localtime.
    # 3. To be linked via post-install.
    rm -f \
     "${destdir}/usr/bin/tzselect" \
     "${destdir}/etc/localtime" \
     "${destdir}/etc/zoneinfo-posix"

    # Create .nograft to copy via post-installation the time zone files
    # instead of being linked to the package directory; this is to ensure
    # that the database is always up to date after a package update.  :-)

    find "${destdir}"/etc/zoneinfo* -type d -print | \
     while read -r directory
     do
         ( cd -- "$directory" && touch .nograft )
     done

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

